//
//  dropdownListView.m
//  webViewLocal
//
//  Created by WJC on 2021/5/12.
//

#import "dropdownListView.h"
#import "WJCDefine.h"

#import "UIButton+FCUrlPath.h"

@interface dropdownListView()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UILabel *textLabel;

@end

@implementation dropdownListView
-(void)setInitHeight:(CGFloat)initHeight{
    _initHeight = initHeight;
    [self.tableView reloadData];
}
#pragma mark - Init
-(UIButton *)choicedBtn{
    if (!_choicedBtn) {
        createCommonBtn(btn, font15, [UIColor blackColor])
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        btn.frame = CGRectMake(8, 0, self.width-16-20, 44);
        _choicedBtn = btn;
        WJCWeakSelf
        [_choicedBtn addTargetWithBlock:^(UIButton * _Nonnull button) {
            weakSelf.select = !weakSelf.select;
        }];
    }
    return _choicedBtn;
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.initHeight =frame.size.height;
        self.tableView = [[FCSuitTableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) style:UITableViewStylePlain];
        [self addSubview:self.tableView];
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        self.tableView.cellHeight =[NSString stringWithFormat:@"%lf",self.initHeight?self.initHeight:44];

        WJCWeakSelf
        self.tableView.backgroundColor =  HEXC(0xF5F5F5);
        self.tableView.setTableViewHeadBlock = ^CGFloat(NSInteger section, UIView *headView) {

            headView.backgroundColor = weakSelf.tableView.backgroundColor;;
            UIImageView *arrowDownImg = [[UIImageView alloc] initWithFrame:CGRectMake(weakSelf.width-20-16, ((weakSelf.initHeight?weakSelf.initHeight:44)-20)/2, 20, 20)];
            arrowDownImg.image = [UIImage imageNamed:@"arrow_down"];
            if(weakSelf.select){
                arrowDownImg.transform = CGAffineTransformMakeRotation( M_PI);
            }
            [headView addSubview:arrowDownImg];
            weakSelf.choicedBtn.height = weakSelf.initHeight?weakSelf.initHeight:44;
            if (!weakSelf.selectedItem) {
                if (weakSelf.choicedBtn.titleLabel.text.length<1) {
                    [weakSelf.choicedBtn setTitle:@"请选择" forState:UIControlStateNormal];
                    [weakSelf.choicedBtn setTitleColor:HEXC(0x646262) forState:UIControlStateNormal];
                }
                
            }else{
                
                [weakSelf.choicedBtn setTitle:weakSelf.selectedItem.Name forState:UIControlStateNormal];
                [weakSelf.choicedBtn setTitleColor:HEXC(0x232323) forState:UIControlStateNormal];
                if (weakSelf.selectedItem.Icon) {
                    [weakSelf.choicedBtn setImage:[UIImage imageNamed:weakSelf.selectedItem.Icon] forState:UIControlStateNormal];
                    [weakSelf.choicedBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -4, 0, 6)];
                }
            }
            
            [headView addSubview:weakSelf.choicedBtn];
           
            return weakSelf.initHeight?weakSelf.initHeight:44;
        };
        self.tableView.cellForRowAtIndexPathBlock = ^UITableViewCell *(NSIndexPath *index) {
            UITableViewCell *cell = [weakSelf.tableView dequeueReusableCellWithIdentifier:@"cell"];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
            }
            cell.contentView.backgroundColor = weakSelf.backgroundColor;
            cell.textLabel.text = ((WJCSiftModel*)weakSelf.tableView.contentModelArr[index.row]).Name;
            if (((WJCSiftModel*)weakSelf.tableView.contentModelArr[index.row]).Icon) {
                cell.imageView.image = [UIImage imageNamed:((WJCSiftModel*)weakSelf.tableView.contentModelArr[index.row]).Icon];
            }
            return cell;
        };
        self.tableView.tableViewDidSelect = ^(NSIndexPath *index) {
            WJCSiftModel *sift =weakSelf.dataSource[index.row];
            weakSelf.selectedItem = sift;
            [weakSelf.tableView reloadData];
            if (weakSelf.dropdownListSelect) {
                weakSelf.dropdownListSelect(weakSelf);
            }
        };
    }
    return self;
}
-(void)setSelectedItem:(WJCSiftModel * _Nonnull)selectedItem{
    _selectedItem = selectedItem;
    self.select = NO;
}
-(void)setSelect:(BOOL)select{
    _select = select;
    if (!_select) {
        [UIView animateWithDuration:0.5 animations:^{
            self.height = self.initHeight;
            self.tableView.height = self.initHeight;
        }];
        
        self.tableView.contentModelArr =nil;
    }else{
        [UIView animateWithDuration:0.5 animations:^{
            self.height = 180;
            self.tableView.height = 180;
        }];
        self.tableView.contentModelArr = self.dataSource.mutableCopy;
        if (self.selectedItem) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if (self.tableView.contentModelArr.count>0&&[self.tableView.contentModelArr containsObject:self.selectedItem]) {
                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.tableView.contentModelArr indexOfObject:self.selectedItem] inSection:0];
                    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
                }
            });
        }
    }
    [self.tableView reloadData];
   
}


- (void)layoutSubviews {
    [super layoutSubviews];
}





@end
